package view_api

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)


func GetOrderById(c *gin.Context){
	var userOrder model.UserOrder
	_ = c.ShouldBindQuery(&userOrder)
	if err, reuserOrder := service.GetUserOrder(userOrder.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reuserOrder": reuserOrder}, c)
	}

}
func CreateUserOrder(c *gin.Context){
	var userOrder model.UserOrder
	_ = c.ShouldBindJSON(&userOrder)
	userOrder.OrderSn = fmt.Sprintf("%s%d",time.Now().Format(`20060102150405`),userOrder.BuyId)
	if err := service.CreateUserOrder(userOrder); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func GetUserOrderList(c *gin.Context){
	var pageInfo request.UserOrderSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetUserOrderInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func UpdateUserOrder(c *gin.Context){
	var userOrder model.UserOrder
	_ = c.ShouldBindJSON(&userOrder)
	for k,_:= range userOrder.UserOrderDetail{
		userOrder.UserOrderDetail[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderSendPic{
		userOrder.UserOrderSendPic[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderGetPic{
		userOrder.UserOrderGetPic[k].ID = 0
	}
	for k,_:= range userOrder.UserOrderPay{
		userOrder.UserOrderPay[k].ID = 0
	}
	service.DeleteUserOrderPayByOrderID(int(userOrder.ID))
	service.DeleteUserOrderDetailByOrderId(int(userOrder.ID))
	service.DeleteUserOrderSendByOrderId(int(userOrder.ID))
	service.DeleteUserOrderGetPicByOrderId(int(userOrder.ID))
	if err := service.UpdateUserOrder(userOrder); err != nil {

		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func UpdateUserOrderDetail(c *gin.Context){
	var userOrderDetal model.UserOrderDetail
	_ = c.ShouldBindJSON(&userOrderDetal)
	if err := service.UpdateUserOrderDetail(userOrderDetal); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func UserOrderDetailList(c *gin.Context){
	var pageInfo request.UserOrderDetailSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetUserOrderDetailInfoLists(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}